package com.ffcs.crmd.tsp.admin.control;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.exception.ExceptionUtil;
import com.ffcs.crmd.platform.control.AbstractController;
import com.ffcs.crmd.platform.pub.vo.RetVo;
import com.ffcs.crmd.tsp.admin.api.dto.ClusterDTO;
import com.ffcs.crmd.tsp.admin.api.facade.IClusterFacade;

/**
 * 
 * 功能说明:集群控制层
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年2月16日 下午4:49:00
 *
 * 版本号 | 作者 | 修改时间 | 修改内容
 *
 */
@Controller
@ResponseBody
@RequestMapping("/tsp/cluster")
public class ClusterController extends AbstractController {
    
    private static final Logger logger = LoggerFactory.getLogger(ClusterController.class);
    
    @Autowired
    private IClusterFacade      clusterFacade;
    
    /**
     * 
     * 功能说明:新增集群
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/select")
    public RetVo select(ModelMap map, HttpSession httpSession, @RequestBody ClusterDTO clusterDTO) {
        RetVo retVo = new RetVo(true);
        try {
            if (StringUtils.isNoneBlank(clusterDTO.getName())) {
                httpSession.setAttribute("clusterName", clusterDTO.getName());
            } else {
                httpSession.removeAttribute("clusterName");
            }
        } catch (Exception e) {
            logger.error("Cluster Action : add clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:新增集群
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/add")
    public RetVo add(ModelMap map, HttpSession httpSession, @RequestBody ClusterDTO clusterDTO) {
        RetVo retVo = new RetVo(true);
        try {
            clusterFacade.save(clusterDTO);
        } catch (Exception e) {
            logger.error("Cluster Action : add clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:删除集群
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/deleteList")
    public RetVo deleteList(ModelMap map, HttpSession httpSession, @RequestBody List<ClusterDTO> clusterDtoList) {
        RetVo retVo = new RetVo(true);
        try {
            clusterFacade.remove(clusterDtoList);
        } catch (Exception e) {
            logger.error("Cluster Action : delete clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    /**
     * 
     * 功能说明:删除集群
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/delete")
    public RetVo delete(ModelMap map, HttpSession httpSession, @RequestBody ClusterDTO clusterDto) {
        RetVo retVo = new RetVo(true);
        try {
            clusterFacade.remove(clusterDto);
        } catch (Exception e) {
            logger.error("Cluster Action : delete clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:更新集群
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/update")
    public RetVo update(ModelMap map, HttpSession httpSession, @RequestBody ClusterDTO clusterDTO) {
        RetVo retVo = new RetVo(true);
        try {
            clusterFacade.update(clusterDTO);
        } catch (Exception e) {
            logger.error("Cluster Action : update clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
   /**
    * 
    * 功能说明:
    * 
    * @author ZHONGFUHUA
    *
    * @Date 2016年2月17日
    *
    */
    @RequestMapping(value = "/queryCurrent")
    public RetVo queryCurrent(ModelMap map, HttpSession httpSession) {
        RetVo retVo = new RetVo(true);
        try {
            String clusterName = String.valueOf(httpSession.getAttribute("clusterName"));
            retVo.setObject(clusterName);
        } catch (Exception e) {
            logger.error("Cluster Action : query all clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    
    /**
    * 
    * 功能说明:
    * 
    * @author ZHONGFUHUA
    *
    * @Date 2016年2月17日
    *
    */
    @RequestMapping(value = "/query")
    public RetVo query(ModelMap map, HttpSession httpSession) {
        RetVo retVo = new RetVo(true);
        try {
            List<ClusterDTO> clusterList = clusterFacade.query();
            retVo.setDataList(clusterList);
        } catch (Exception e) {
            logger.error("Cluster Action : query all clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @author ZHONGFUHUA
     *
     * @Date 2016年2月17日
     *
     */
    @RequestMapping(value = "/queryPage")
    public RetVo queryPage(ModelMap map, HttpSession httpSession, @RequestBody ClusterDTO clusterDTO) {
        RetVo retVo = new RetVo(true);
        try {
            PageInfo<ClusterDTO> pageInfo = clusterFacade.queryPage(clusterDTO.getName(), clusterDTO.getPageNumber(), clusterDTO.getPageSize());
            retVo.setPageInfo(pageInfo);
        } catch (Exception e) {
            logger.error("Cluster Action : query all page clueter exception:", e);
            retVo.setResult(false);
            retVo.setMsgTitle(e.getMessage());
            retVo.setDetailMsg(ExceptionUtil.getTraceInfo(e));
        }
        return retVo;
    }
}
